/*
 * protocol.c
 *
 * Created: 20-02-2012 15:02:41
 *  Author: Bjarke
 */ 

#include "protocol.h"
#include <string.h>

char chcksum(char* chk, int size)
{
	char retVal = 0;
	int i;
	for(i = 0; i< size; i++)
	{
		// Send the character pointed to by "Streng"
		retVal ^= *chk;
		// Advance the pointer one step
		chk++;		
	}
	return retVal;
}

void SendData(char type, char* text)
{
	char dataToSend[30] ;
	char i = 0;
	while(text[i] != 0)
	{
		i++;
	}
	dataToSend[0] = type;
	dataToSend[1] = i;
	memccpy(&dataToSend[2],text,i,30);
	dataToSend[2+i] = chcksum(dataToSend, i+2);
	dataToSend[3+i] = 's';
	dataToSend[4+i] = 't';
	dataToSend[5+i] = 'o';
	dataToSend[6+i] = 'p';
	SendArray(dataToSend,6+i);
}

void dataReceived(char* data, int size)
{
	SendArray(data,size);	
}